package com.unism.util;

import java.util.Iterator;
import java.util.TreeMap;

import com.unism.objType.ObjectType;
import com.unism.objType.ObjectTypeConfig;

public class InstanceType {
	
	

	private int instanceType= 0;
	/**
	 * 生成实例类型                            实例类型 =(00+实例状态+设备类型+对象类型)       
	 * @param status
	 * @param deviceType
	 * @param objType
	 * @return
	 */
	public static Integer calcInstanceType(InstanceStatus status,Integer deviceType,Integer objType){
		
		Integer instanceType=null;
		
		Boolean a=isEffectiveByObjType(objType);
		Boolean b=isEffectiveByDeviceType(deviceType) ;
		
		if( a && b){
		    instanceType = 0x00000000;
			instanceType |= status.getValue();
			instanceType <<= 8 ;
			instanceType |= deviceType ;
			instanceType <<= 8 ;	
			
			if( null == objType){
			
				instanceType |= 0 ;
			}else{
				instanceType |= objType ;
			}
		}
		return instanceType;
	}
		
	private static Boolean  isEffectiveByDeviceType(Integer deviceType){
		
		TreeMap<Integer, ObjectType> map=ObjectTypeConfig.enableMap;
		Iterator it=map.keySet().iterator();
		
        while(it.hasNext()){
    		
        	ObjectType objType=map.get(it.next());
        	if(objType !=null && objType.getDeviceType() == deviceType){
        		
        		return true;
        	}
    	}
       return false; 
	}
	
    private static Boolean  isEffectiveByObjType(Integer objType){
		
    	if( null == objType){
    		
    		return true;
    	}
    	TreeMap<Integer, ObjectType> map=ObjectTypeConfig.enableMap;
    	
    	Iterator it=map.keySet().iterator();
    	
    	while(it.hasNext()){
    		
    		if(it.next() == objType ){
    			return true;
    		}
    	}
    	 
		return false;
		
	}
  //=================================================
  //=================================================
	/**
	 * 获取实例状态
	 * @param instanceType
	 * @return
	 */
	public static InstanceStatus getInstanceStatus(Integer instanceType) {
		switch (instanceType>>16) {
		case 1:
			return InstanceStatus.ON_DEBUG;
		case 2:
			return InstanceStatus.ON_START;
		default:
			return InstanceStatus.ON_ERROR;
		}
	}
	
	/**
	 * 获取设备类型
	 * @param instanceType
	 * @return
	 */
	public static Integer getDeviceType(Integer instanceType) {
		int temp = 0x0000FF00;
		return (instanceType&temp)>>8;
	}
	/**
	 * 获取对象类型
	 * @param instanceType
	 * @return
	 */
	public  static Integer getObjType(Integer instanceType) {
		int temp = 0x000000FF;
		return instanceType&temp;
	}
	/**
	 * 获取设备状态
	 * @param instanceType
	 * @return
	 */
	public static Integer setInstanceStatus(Integer instanceType,InstanceStatus status) {
		int dType = status.getValue() & 0x000000FF;
		dType = dType << 16;
		int iType = instanceType & 0xFF00FFFF;
		return dType | iType;
	}
	
	/**
	 * 获取设备类型
	 * @param instanceType
	 * @return
	 */
	public static int setDeviceType(Integer instanceType,Integer deviceType) {
		int dType = deviceType & 0x000000FF;
		dType = dType << 8;
		int iType = instanceType & 0xFFFF00FF;
		return dType | iType;
	}
	/**
	 * 获取对象类型
	 * @param instanceType
	 * @return
	 */
	public  static Integer setObjType(Integer instanceType,Integer objType) {
		int oType = objType & 0x000000FF;
		int iType = instanceType & 0xFFFFFF00;
		return oType | iType;
	}

	/**
	 * 输出类型信息
	 * @param instancetype
	 * @return
	 */
	public  static void printType(Integer instancetype) {
		System.out.println("实例类型为：" + instancetype);
		System.out.print("实例的设备类型为：" + getDeviceType(instancetype));
		System.out.print("实例的对象类型为：" + getObjType(instancetype));
		System.out.println("实例的状态为：" + getInstanceStatus(instancetype));
	}
	public Integer getInstanceType() {
		return instanceType;
	}
	public void setInstanceType(Integer instanceType) {
		this.instanceType = instanceType;
	}
	public static void main(String[] args) {
		int itype = InstanceType.calcInstanceType(InstanceStatus.ON_DEBUG, 1, 1);
		InstanceType.printType(itype);
		
	}
	
}
